﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Windows.Forms;
using Common;


namespace Visits.Model
{
    public class GraphicsVisitModel
    {
        private DataSet _visits = null;
        private SqlCeDataAdapter grapDataAdapter;


        public GraphicsVisitModel()
        {
            string sqlQuery = "SELECT g.BuyPointId, g.Sunday, g.Monday, g.Tuesday, g.Wednesday, g.Thursday, g.Friday, g.Saturday, b.NameBuyPoint, b.Adress" +
                " FROM GraphicsVisitor g INNER JOIN BuyPoints AS b ON g.BuyPointId = b.IdBuyPoint";
            _visits = new DataSet();
            grapDataAdapter = new SqlCeDataAdapter(sqlQuery, WorkDataBase.GetInstance().Connection);

            sqlQuery = "delete GraphicsVisitor where BuyPointId = @BuyPointId";
            grapDataAdapter.DeleteCommand = new SqlCeCommand(sqlQuery, WorkDataBase.GetInstance().Connection);
            grapDataAdapter.DeleteCommand.Parameters.Add("@BuyPointId", SqlDbType.Int,0,"BuyPointId");

            sqlQuery =
                "insert into GraphicsVisitor (BuyPointId, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday) values (@BuyPointId, @Sunday, @Monday, @Tuesday, @Wednesday, @Thursday, @Friday, @Saturday)";
            grapDataAdapter.InsertCommand = new SqlCeCommand(sqlQuery, WorkDataBase.GetInstance().Connection);
            grapDataAdapter.InsertCommand.Parameters.Add("@BuyPointId", SqlDbType.Int, 0, "BuyPointId");
            grapDataAdapter.InsertCommand.Parameters.Add("@Sunday", SqlDbType.Bit, 0, "Sunday");
            grapDataAdapter.InsertCommand.Parameters.Add("@Monday", SqlDbType.Bit, 0, "Monday");
            grapDataAdapter.InsertCommand.Parameters.Add("@Tuesday", SqlDbType.Bit, 0, "Tuesday");
            grapDataAdapter.InsertCommand.Parameters.Add("@Wednesday", SqlDbType.Bit, 0, "Wednesday");
            grapDataAdapter.InsertCommand.Parameters.Add("@Thursday", SqlDbType.Bit, 0, "Thursday");
            grapDataAdapter.InsertCommand.Parameters.Add("@Friday", SqlDbType.Bit, 0, "Friday");
            grapDataAdapter.InsertCommand.Parameters.Add("@Saturday", SqlDbType.Bit, 0, "Saturday");

            sqlQuery =
                "Update GraphicsVisitor set Sunday = @Sunday, Monday = @Monday, Tuesday = @Tuesday, Wednesday = @Wednesday, Thursday = @Thursday, Friday = @Friday, Saturday = @Saturday where BuyPointId = @BuyPointId";
            grapDataAdapter.UpdateCommand = new SqlCeCommand(sqlQuery, WorkDataBase.GetInstance().Connection);
            grapDataAdapter.UpdateCommand.Parameters.Add("@BuyPointId", SqlDbType.Int, 0, "BuyPointId");
            grapDataAdapter.UpdateCommand.Parameters.Add("@Sunday", SqlDbType.Bit, 0, "Sunday");
            grapDataAdapter.UpdateCommand.Parameters.Add("@Monday", SqlDbType.Bit, 0, "Monday");
            grapDataAdapter.UpdateCommand.Parameters.Add("@Tuesday", SqlDbType.Bit, 0, "Tuesday");
            grapDataAdapter.UpdateCommand.Parameters.Add("@Wednesday", SqlDbType.Bit, 0, "Wednesday");
            grapDataAdapter.UpdateCommand.Parameters.Add("@Thursday", SqlDbType.Bit, 0, "Thursday");
            grapDataAdapter.UpdateCommand.Parameters.Add("@Friday", SqlDbType.Bit, 0, "Friday");
            grapDataAdapter.UpdateCommand.Parameters.Add("@Saturday", SqlDbType.Bit, 0, "Saturday");
        }

        public void SaveChanges()
        {
            grapDataAdapter.Update(_visits, "GraphicsVisits");
        }


        public DataSet Visit
        {
            get
            {
                Load();
                return _visits;
            }
        }

        private void Load()
        {
            _visits.Clear();
            grapDataAdapter.Fill(_visits, "GraphicsVisits");
        }
    }
}
